﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WeiBo.Models;

namespace WeiBo.Controllers
{
    public class EnrollController : Controller
    {
        /// <summary>
        /// 随机生成数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string SJS(int length = 6)
        {

            var result = new StringBuilder();
            for (var i = 0; i < length; i++)
            {
                var r = new Random(Guid.NewGuid().GetHashCode());
                result.Append(r.Next(0, 10));
            }
            return result.ToString();
        }

        // GET: Enroll
        public ActionResult EnrollIndex()
        {
            return View();
        }
        [HttpPost]
        public ActionResult EnrollIndex(string WBName, string WBPwd)
        {
            try
            {
                using (WeiBoEntities db = new WeiBoEntities())
                {
                    string WBID = SJS();
                    string name = SJS();
                    var pwd = int.Parse(WBPwd);
                    var weiboInfo = new WeiBoInfo() { WBID = WBID, WBName = WBName, WBPwd = pwd };
                    var grInfo = new GeRenInfo() { WBID = WBID, GRName = "用户" + name, GRImg = "morentouxiang.jpg" };
                    db.WeiBoInfo.Add(weiboInfo);
                    db.GeRenInfo.Add(grInfo);
                    var count = db.SaveChanges();
                    if (count > 0)
                    {
                        return RedirectToAction("GRInfoIndex", new { WBID });
                    }
                    else
                    {
                        return View();
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        public ActionResult GRInfoIndex(string WBID)
        {
            ViewBag.WBID = WBID;
            if (WBID == null)
            {
                return RedirectToAction("EnrollIndex");
            }
            System.Web.HttpContext.Current.Session["id"] = WBID;
            return View();
        }
        [HttpPost]
        public ActionResult GRInfoIndex(string GRName, string GRJianJie)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                if (Request.Files.Count > 0)
                {
                    //获取文件
                    var file = HttpContext.Request.Files[0];
                    //获取上传文件的扩展名
                    //首先将文件名按.切割
                    var temp = file.FileName.Split('.');
                    //获取扩展名
                    var extendName = temp[temp.Length - 1];
                    if (extendName == "jpg" || extendName == "png" || extendName == "jpg" || extendName == "jpeg")
                    {
                        //获取路径
                        var path = HttpContext.Server.MapPath("~/TouXiang/");
                        //判断路径是否存在
                        if (Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        //文件重新命名
                        var name = DateTime.Now.ToFileTime() + "." + extendName;
                        //保存上传文件
                        file.SaveAs(Path.Combine(path, name));
                        //保存到数据库
                        var GRInfo = db.GeRenInfo.FirstOrDefault(p => p.WBID == WBID);
                        GRInfo.GRName = GRName;
                        GRInfo.GRJianJie = GRJianJie;
                        GRInfo.GRImg = name;
                        db.SaveChanges();
                        //返回结果
                        return RedirectToAction("SquareIndex", "Home", new { WBID });
                    }
                    else if (GRName != null || GRJianJie != null)
                    {
                        //保存到数据库
                        var GRInfo = db.GeRenInfo.FirstOrDefault(p => p.WBID == WBID);
                        GRInfo.GRName = GRName;
                        GRInfo.GRJianJie = GRJianJie;
                        db.SaveChanges();
                        //返回结果
                        return RedirectToAction("SquareIndex", "Home", new { WBID });
                    }
                    else
                    {
                       return View();
                    }
                }
                else if (GRName != null || GRJianJie != null)
                {
                    //保存到数据库
                    var GRInfo = db.GeRenInfo.FirstOrDefault(p => p.WBID == WBID);
                    GRInfo.GRName = GRName;
                    GRInfo.GRJianJie = GRJianJie;
                    db.SaveChanges();
                    //返回结果
                    return RedirectToAction("SquareIndex", "Home", new { WBID });
                }
                else
                {
                    return View();
                }
            }

        }

    }
}